package com.yixun.qfbao.hander;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.api.service.CompanyService;
import com.yixun.qfbao.apidto.GetDeptByCompCodeReqDto;
import com.yixun.qfbao.apidto.GetDeptByCompCodeResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.Department;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.dto.DepartmentDto;
import com.yixun.qfbao.dao.DepartmentDao;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.vo.GetDeptByCompCodeReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @Description
 * @Date 2018-9-19
 * @Author JHSong
 * @Computer Administrator
 */
@Service("getDeptByCompCodeHander")
public class GetDeptByCompCodeHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetDeptByCompCodeHander.class);

	@Autowired
	private CompanyService companyService;
    @Autowired
    private DepartmentDao departmentDao;
    
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetDeptByCompCodeReqVo req = (GetDeptByCompCodeReqVo)reqDto;
        GetDeptByCompCodeReqDto reqDto1 = req.getParams();
        BaseResponseDTO<GetDeptByCompCodeResDto> res = new BaseResponseDTO<GetDeptByCompCodeResDto>();
        GetDeptByCompCodeResDto resDto = new GetDeptByCompCodeResDto();
        try {
        	List<Department> returnList = new ArrayList<Department>();
        	if(StringUtils.isBlank(reqDto1.getCompanyCode())) {
        		resDto.setDeptList(returnList);
        		resDto.setResult(ResultEnum.FAIL.getNo());
        		res.setResData(resDto);
                res.setResult(ResultEnum.FAIL.getNo());
                return res;
        	}else{
                reqDto1.setCompanyCode(reqDto1.getCompanyCode().toUpperCase());
            }
        	CompanyDto compDto = new CompanyDto();
        	compDto.setCompanyCode(reqDto1.getCompanyCode());
        	Company company = companyService.selectByDto(compDto);
        	if(null == company) {
        		resDto.setDeptList(returnList);
                resDto.setResult(ResultEnum.FAIL.getNo());
        		res.setResData(resDto);
                res.setResult(ResultEnum.FAIL.getNo());
                return res;
        	}
        	DepartmentDto deptDto = new DepartmentDto();
        	deptDto.setMerchantId(company.getId());
        	if(StringUtils.isNotBlank(reqDto1.getMendian())) {
        		deptDto.setParentId(Long.valueOf(reqDto1.getMendian()));
        	}
            List<Department> deptList = departmentDao.selectListByDto(deptDto);
            
            //根据类型获取类型
            returnList = departmentDao.getDeptByCompCode(deptList, reqDto1.getDeptType());
            resDto.setDeptList(returnList);
            resDto.setResult(ResultEnum.SUCCESS.getNo());
        }catch(TServerException e){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg(e.getErrorMsg());
        } catch (Exception e) {
            logger.error("GetDeptByCompCodeHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("系统异常");
        }
        res.setResData(resDto);
        res.setResult(resDto.getResult());
        return res;
    }
}
